<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.util.*, com.wzu.pojo.Reader, com.wzu.service.ReaderServiceImpl" %>
<%
    // 获取当前页面标识
    String currentPage = request.getRequestURI().substring(request.getContextPath().length());

    // 获取读者数据
    ReaderServiceImpl readerService = new ReaderServiceImpl();
    List<Reader> readerList = readerService.getAllReaders();
%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>读者列表</title>
    <style>
        /* 保持与bookList.jsp完全相同的样式定义 */
        input[type="submit"], .button-link {
            width: 100%; padding: 10px; border: none; border-radius: 4px;
            background-color: #4CAF50; color: white; cursor: pointer;
            text-decoration: none; display: inline-block; text-align: center;
        }

        input[type="submit"]:hover, .button-link:hover {
            background-color: #45a049;
        }

        .button-container { text-align: center; margin-top: 15px; }
        body, html { height: 100%; margin: 0; font-family: Arial, sans-serif; }

        .sidebar {
            width: 90px; background-color: #333; color: white; padding: 15px;
            position: fixed; top: 0; left: 0; height: 100%;
        }

        .sidebar h2 { padding: 10px; color: #fff; }
        .sidebar ul { list-style-type: none; padding: 0; }

        .sidebar ul li a {
            color: white; padding: 10px; display: block; text-decoration: none;
        }

        .sidebar ul li a:hover { background-color: #575757; }

        .sidebar ul li a.active {
            background-color: #4CAF50; /* 设置当前页面的背景颜色 */
        }

        .main-content { margin-left: 120px; padding: 20px; }
        .header { background-color: #333; color: white; text-align: center; padding: 10px 0; }

        .content { padding: 20px; }

        table {
            width: 100%; border-collapse: collapse; margin-top: 20px;
        }

        th, td {
            border: 1px solid #ddd; padding: 8px; text-align: left;
        }

        th { background-color: #f2f2f2; }
    </style>
</head>
<body>
<div class="sidebar">
    <h2>菜单</h2>
    <ul>
        <li><a href="bookList.jsp"
               class="<%= currentPage.equals("/application/bookList.jsp") ? "active" : "" %>">
            图书列表</a></li>
        <li><a href="addBook.jsp"
               class="<%= currentPage.equals("/application/addBook.jsp") ? "active" : "" %>">
            添加图书</a></li>
        <li><a href="borrowRecord.jsp"
               class="<%= currentPage.equals("/application/borrowRecord.jsp") ? "active" : "" %>">
            借阅记录</a></li>
        <li><a href="readerList.jsp"
               class="<%= currentPage.equals("/application/readerList.jsp") ? "active" : "" %>">
            读者列表</a></li>
        <li id="adminLink" style="display: none;">
            <a href="bookManagement.jsp"
               class="<%= currentPage.equals("/application/bookManagement.jsp") ? "active" : "" %>">
                管理入口</a></li>
        <li><a href="PersonalData.jsp"
               class="<%= currentPage.equals("/application/PersonalData.jsp") ? "active" : "" %>">
            个人中心</a></li>
        <li><a href="<%=request.getContextPath()%>/login.jsp">退出登录</a></li>
    </ul>
</div>

<div class="main-content">
    <div class="header">
        <a>图书信息管理系统 - 读者列表</a>
    </div>
    <div class="content">
        <h2>读者列表</h2>
        <table>
            <thead>
            <tr>
                <th>读者编号</th>
                <th>读者姓名</th>
                <th>性别</th>
                <th>年龄</th>
                <th>联系方式</th>
                <th>证件类型</th>
                <th>证件号码</th>
            </tr>
            </thead>
            <tbody>
            <%
                if (readerList != null && !readerList.isEmpty()) {
                    for (Reader r : readerList) {
            %>
            <tr>
                <td><%=r.getReader_id()%></td>
                <td><%=r.getName()%></td>
                <td><%=r.getGender()%></td>
                <td><%=r.getAge()%></td>
                <td><%=r.getContact_info()%></td>
                <td><%=r.getId_type()%></td>
                <td><%=r.getId_number()%></td>
            </tr>
            <%
                }
            } else {
            %>
            <tr><td colspan="11" style="text-align:center;">暂无读者信息</td></tr>
            <%
                }
            %>
            </tbody>
        </table>
    </div>
</div>

<script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
<script>
    // 判断是否管理员显示入口
    let userInfo = sessionStorage.getItem('userInfo');
    if (userInfo) {
        userInfo = JSON.parse(userInfo);
        document.getElementById('adminLink').style.display =
            userInfo.isroot === 1 ? 'list-item' : 'none';
    }

    // 高亮当前菜单项（可选增强）
    const currentPath = window.location.pathname;
    document.querySelectorAll('.sidebar ul li a').forEach(link => {
        if (link.getAttribute('href') === currentPath) {
            link.classList.add('active');
        }
    });
</script>
</body>
</html>
